package com.regretbuying.front.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.regretbuying.front.service.categories.CategoriesService;
import com.regretbuying.front.service.goods.GoodsService;
import com.regretbuying.front.service.shopping_cart.CartService;
import com.regretbuying.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Resource(name="fcasi")
    private CartService cartService;

    @Resource(name = "fcsi")
    private CategoriesService cateService;

    @Resource(name = "fgsi")
    private GoodsService goodService;

    @RequestMapping("/order")
    public String TestOrder(){
        return "front/system/system_order";
    }

    /**
     * 商品详情
     * @param id
     * @param goodsSpecs
     * @param model
     * @return
     */
    @RequestMapping("/system_detailed/{id}")
    public String TestDetailed(@PathVariable int id,
                               @RequestParam(defaultValue = "0")int goodsSpecs,
                               Model model){

        Goods goods=goodService.findGoodsById(id);
        model.addAttribute("goods",goods);

        Specs specs=goodService.findSprcsById(id,goodsSpecs);
        model.addAttribute("specs",specs);

        Categories cate1 = cateService.CategoriesById(goods.getOneLevelMenu());
        model.addAttribute("cate1",cate1);

        Categories cate2 = cateService.CategoriesById(goods.getTwoLevelMenu());
        model.addAttribute("cate2",cate2);

        List<Specs> specsList=goodService.getSpecsGoodsId(goods.getGoodsId());
        model.addAttribute("specsList",specsList);

//        对应的商品规格  我懒得插数据 所有商品用同一条
//        Introduction intro = goodService.findIntroductionByGoodsId(goods.getGoodsId());
        Introduction intro = goodService.getIntroductionByGoodsId(4);
        String[] inttoString = intro.getIntroName().split(",");
        model.addAttribute("inttoString",inttoString);

        //评论条数
        int count = goodService.getCountByGoodsId(goods.getGoodsId());
        model.addAttribute("count",count);

        List<Comment> commentsList = goodService.getCommentByGoodsId(goods.getGoodsId());
        model.addAttribute("commentsList",commentsList);

        return "front/system/system_detailed";
    }

    /**
     * 商品详情
     * @param id
     * @param goodsSpecs
     * @param model
     * @return
     */
    @RequestMapping("/system_countdown/{id}")
    public String TestCountdown(@PathVariable int id,
                               @RequestParam(defaultValue = "0")int goodsSpecs,
                               Model model){

        Goods goods=goodService.findGoodsById(id);
        model.addAttribute("goods",goods);

        Specs specs=goodService.findSprcsById(id,goodsSpecs);
        model.addAttribute("specs",specs);

        Categories cate1 = cateService.CategoriesById(goods.getOneLevelMenu());
        model.addAttribute("cate1",cate1);

        Categories cate2 = cateService.CategoriesById(goods.getTwoLevelMenu());
        model.addAttribute("cate2",cate2);

        List<Specs> specsList=goodService.getSpecsGoodsId(goods.getGoodsId());
        model.addAttribute("specsList",specsList);

//        对应的商品规格  我懒得插数据 所有商品用同一条
//        Introduction intro = goodService.findIntroductionByGoodsId(goods.getGoodsId());
        Introduction intro = goodService.getIntroductionByGoodsId(4);
        String[] inttoString = intro.getIntroName().split(",");
        model.addAttribute("inttoString",inttoString);

        //评论条数
        int count = goodService.getCountByGoodsId(goods.getGoodsId());
        model.addAttribute("count",count);

        List<Comment> commentsList = goodService.getCommentByGoodsId(goods.getGoodsId());
        model.addAttribute("commentsList",commentsList);

        return "front/system/system_countdown";
    }

    /**
     * 全部商品
     * @param id
     * @param goodsname
     * @param currentPageNo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String TestList(@RequestParam(defaultValue = "0") int id,
                           @RequestParam(defaultValue = "") String goodsname,
                           @RequestParam(defaultValue = "1") int currentPageNo,
                           Model model){

        Map params = new HashMap();
        params.put("id",id);
        params.put("goodsName",goodsname);
        params.put("goodsSpecs",true);

        model.addAttribute("id",id);

        model.addAttribute("gname",goodsname);

        Categories cate = cateService.CategoriesById(id);
        model.addAttribute("cateName",cate.getCateName());

        //初始，设置当前页码和一页取几条数据
        PageHelper.startPage(currentPageNo, 8) ;

        List<Goods> goodsList = goodService.findGoodsByParams(params);
        PageInfo<Goods> page = new PageInfo<>(goodsList) ;
        model.addAttribute("page", page) ;

        return "front/system/system_list";
    }

    /**
     * 主页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = {"/index","/",""})
    public String IndexTest(Model model, HttpSession session){

        //获取一级分类搜索框下导航
        List<Categories> cateList = cateService.getFirstCategories();
        session.setAttribute("cateList",cateList);

        //获取菜单列表所有层级分类
        List<Map<Categories, List<Categories>>> categoriesList = cateService.getCategories();
        session.setAttribute("categoriesList",categoriesList);

        //获取新品热销
        List<Goods> goodsList = goodService.getNewProductSelling();
        model.addAttribute("goodsList",goodsList);

        //获取商品推荐
        List<Goods> goodsRecommend = goodService.getNewRecommend();
        model.addAttribute("goodsRecommend",goodsRecommend);

        List<Goods> goodsDiscounts = goodService.getNewIfDiscounts();
        model.addAttribute("goodsDiscounts",goodsDiscounts);

//        //获取当前登录用户
//        Member member = (Member)session.getAttribute("login");
//        int id = member.getMemberId();
//
//        //根据会员ID获取对应的购物车数据
//        List<Cart> cartList = cartService.getCartMemberById(id);
//        session.setAttribute("indexCart", cartList) ;

        return "front/system/system_index";
    }

    @RequestMapping("/login")
    public String login() {
        return "front/login";
    }
}